package com.wondersgroup.framework.sample.component.form.action;

import com.wondersgroup.framework.core.exception.BusinessException;
import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.data.UserData;
import com.wondersgroup.framework.sample.component.form.vo.UserVO;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;

public class FormAction extends BaseAjaxAction
{
  UserVO userVO;
  private static final String prefix = "{\"success\":true,result:[";
  private static final String suffex = "]}";

  public FormAction()
  {
    this.userVO = new UserVO();
  }

  public String loadForm()
  {
    try
    {
      if (super.getServletRequest().getParameter("lazy") != null)
        Thread.sleep(3000L);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
    String id = getServletRequest().getParameter("id");
    UserVO userVo = new UserData(getServletRequest()).loadUserById(id);
    String json = "{\"success\":true,result:[" + VOUtils.getJsonData(userVo) + "]}";
    createJSonData(json);
    return "ajax";
  }

  public String ajaxSubmit()
  {
    if (new UserData(getServletRequest()).loadUserById(this.userVO.getId()) != null) {
      new UserData(getServletRequest()).updateUser(this.userVO);
    }
    else
      new UserData(getServletRequest()).saveUser(this.userVO);

    printSubmitInfo(this.userVO);
    return "ajax";
  }

  public String loadFormFailed()
  {
    throw new BusinessException("莫名的加载错误!");
  }

  public String ajaxSubmitFailed()
  {
    throw new BusinessException("莫名的提交错误!");
  }

  public ValueObject getValueObject()
  {
    return this.userVO;
  }

  public void setFormVo(UserVO userVO) {
    this.userVO = userVO;
  }

  private void printSubmitInfo(UserVO userVO) {
    this.logger.info("提交成功信息：");
    this.logger.info("编号： " + userVO.getId());
    this.logger.info("姓名： " + userVO.getName());
    this.logger.info("身份证号码： " + userVO.getIdcard());
    this.logger.info("性别： " + userVO.getSex());
    this.logger.info("出生日期： " + userVO.getDateTime());
    this.logger.info("区县： " + userVO.getDistrict());
    this.logger.info("民族： " + userVO.getNation());
    this.logger.info("户籍性质： " + userVO.getCensus());
    this.logger.info("邮政编码： " + userVO.getZip());
    this.logger.info("联系电话： " + userVO.getPhone());
    this.logger.info("电子邮箱： " + userVO.getEmail());
    this.logger.info("户籍所在地： " + userVO.getCensusAddress());
    this.logger.info("开户类型： " + userVO.getCardType());
    this.logger.info("开户银行： " + userVO.getBankName());
    this.logger.info("银行账号： " + userVO.getCardNumber());
    this.logger.info("银行账号： " + userVO.getCash());
  }
}